﻿//------------------------------------------------------------------------------
// System Name:    Customer Service Platform
// Component:      CIS Domestic CompanyManagementHelper
// Created By:     Samara Jiang (samaraj@wicresoft.com)
// Created Date:   2011-1-27
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data.Objects;
using System.Linq;
using System.Security.Principal;
using System.Text;
using ASBCC.Database.EntityFramework;

namespace ASBCC.Business.CIS
{
    /// <summary>
    /// CIS 国内 公司管理模块数据库访问类
    /// </summary>
    public static class CompanyManagementHelper
    {
        /// <summary>
        /// 通过CompanyID查询PCAT
        /// </summary>
        public static IEnumerable GetPCATsWithCompanyRelation(int companyID)
        {
            using (var dc = new CCEntities())
            {
                var temp = from t in dc.PCATs
                           where !t.Province.Disabled && !t.Category.Disabled
                           select new
                           {
                               ProvinceID = t.ProvinceID,
                               CategoryID = t.CateID,
                               ProvinceName = t.Province.Name,
                               CategoryName = t.Category.Name,
                               CompanyCount = t.Companies.Count(c => c.CompanyID == companyID),
                           };

                var source = from t in temp.ToArray()
                             select new
                             {
                                 PCID = t.ProvinceID + ";" + t.CategoryID,
                                 t.ProvinceName,
                                 t.CategoryName,
                                 ContainsCompany = t.CompanyCount == 1 ? "checked" : "",
                             };

                return source.ToArray();
            }
        }

        /// <summary>
        /// 获取添加公司信息的当前用户信息和时间
        /// </summary>
        public static void OnCompanyManagementInserting(ObjectContext dc, Company company, string pcatnames, IPrincipal user)
        {
            var users = dc.CreateQuery<Employee>("[Employees]");
            company.CreateBy = users.First(p => p.NameEN == user.Identity.Name);
            company.LastModifyBy = company.CreateBy;
            company.CreateTime = DateTime.Now;
            company.LastModifyTime = DateTime.Now;
            CompanyManagementInserting(dc, company, pcatnames);
        }


        /// <summary>
        /// 获取修改公司信息的当前用户信息和时间
        /// </summary>
        public static void OnCompanyManagementUpdating(ObjectContext dc, Company company, string pcatnames, IPrincipal user)
        {
            var users = dc.CreateQuery<Employee>("[Employees]");
            company.LastModifyBy = users.First(p => p.NameEN == user.Identity.Name);
            company.LastModifyTime = DateTime.Now;
            company.PCATs.Load();
            company.PCATs.Clear();
            CompanyManagementInserting(dc, company, pcatnames);

        }

        private static void CompanyManagementInserting(ObjectContext dc, Company company, string pcatnames)
        {
            var pcats = dc.CreateQuery<PCAT>("[PCATs]");
            if (!String.IsNullOrEmpty(pcatnames))
            {
                foreach (var pcat in pcatnames.Split(','))
                {
                    int pid = int.Parse(pcat.Split(';')[0]);
                    int cid = int.Parse(pcat.Split(';')[1]);
                    var pcatInCompany = pcats.First(p => p.ProvinceID == pid && p.CateID == cid);

                    company.PCATs.Add(pcatInCompany);

                }
            }

        }

        /// <summary>
        /// 将一个company的激活状态设为未激活
        /// </summary>
        public static void SetCompanyDisabled(int companyID)
        {
            using (var dc = new CCEntities())
            {
                dc.Companies.FirstOrDefault(p => p.CompanyID == companyID).Disabled = true;
                dc.SaveChanges();
            }
        }

        /// <summary>
        /// 根据当前ID获取Name
        /// </summary>
        public static IEnumerable getName(int entityID)
        {
            using (var dc = new CCEntities())
            {
                var name = dc.Companies.FirstOrDefault(p => p.CompanyID == entityID).Name;
                return name;
            }
        }

    }
}
